#!/usr/bin/env python3
# -*- coding: utf-8 -*-

################################################################################

import csv
from datetime import datetime

from matplotlib.figure import Figure
import matplotlib.pyplot as plt

################################################################################
#
# 数据可视化，显示sitka2014-7最高气温，和2014年最高最低气温
# 参考：《python 编程从入门到实践》 16章
#
################################################################################

def weather_mouth():
    """ 2014年7月最高气温
    """
    filename = "sitka_weather_07-2014.csv"
    with open(filename) as f:
        reader = csv.reader(f)
        next(reader)

        dates, highs = [], []
        for row in reader:
            dates.append(datetime.strptime(row[0], r'%Y-%m-%d'))
            highs.append(int(row[1]))

    # 设置x轴左右无空隙
    plt.rcParams['axes.xmargin'] = 0
    # 构建可视化窗口
    # dpi,分辨率
    # figsize, 窗口大小
    fig:Figure = plt.figure(dpi=128, figsize=(10, 6))
    # 设置数据及线条颜色
    plt.plot(dates, highs, c='red')
    # plt.plot(highs, c='red')
    # 设置标题
    plt.title("Daily high temperatures, July 2014", fontsize=24)
    # 设置x轴标签
    plt.xlabel('', fontsize=16)
    # 设置斜的日期格式
    fig.autofmt_xdate()
    # 设置y轴标签
    plt.ylabel("Temperature (F)", fontsize=16)
    # 设置刻度线样式
    plt.tick_params(axis="both", which="major", labelsize=16)
    plt.show()

def weather_year():
    """ 2014年最高最低气温
    """
    filename = "sitka_weather_2014.csv"
    with open(filename) as f:
        reader = csv.reader(f)
        next(reader)

        dates, highs, low = [], [], []
        for row in reader:
            dates.append(datetime.strptime(row[0], r'%Y-%m-%d'))
            highs.append(int(row[1]))
            low.append(int(row[3]))

    # 设置x轴左右无空隙
    plt.rcParams['axes.xmargin'] = 0
    # 构建可视化窗口
    # dpi,分辨率
    # figsize, 窗口大小
    fig:Figure = plt.figure(dpi=128, figsize=(10, 6))
    # 设置数据及线条颜色
    plt.plot(dates, highs, c='red')
    plt.plot(dates, low, c='blue')
    plt.fill_between(dates, highs, low, facecolor="blue", alpha=0.1)
    # 设置标题
    plt.title("Daily high temperatures - 2014", fontsize=24)
    # 设置x轴标签
    plt.xlabel('', fontsize=16)
    # 设置斜的日期格式
    fig.autofmt_xdate()
    # 设置y轴标签
    plt.ylabel("Temperature (F)", fontsize=16)
    # 设置刻度线样式
    plt.tick_params(axis="both", which="major", labelsize=16)
    plt.show()


if __name__ == "__main__":
    # weather_mouth()

    weather_year()
